package top.hkyzf.study.leetcode.array;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 朱峰
 * @date 2022-2-15 11:12
 */
public class 矩阵中的幸运数_1380 {
    public List<Integer> luckyNumbers (int[][] matrix) {
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < matrix.length; i++) {
            int index = 0;
            int min = Integer.MAX_VALUE;
            // 查找当前行最小值
            for (int j = 0; j < matrix[0].length; j++) {
                if (min > matrix[i][j]) {
                    min = matrix[i][j];
                    index = j;
                }
            }
            // 判断是否是此列最大值
            for (int j = 0; j < matrix.length; j++) {
                if (min < matrix[j][index]) {
                    break;
                }
                if (j == matrix.length-1) {
                    result.add(min);
                }
            }
        }
        return result;
    }

    @Test
    public void testLuckyNumbers () {
        int[][] matrix = {{3,7,8},{9,11,13},{15,16,17}};
        System.out.println(luckyNumbers(matrix));
        for (int[] mat : matrix) {
            System.out.println(Arrays.toString(mat));
        }
    }
}
